package com.baidu.infinity.ui.fragment.home.view.backgroundpopupview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager.LayoutParams
import android.widget.PopupWindow
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.baidu.infinity.R
import com.baidu.infinity.databinding.LayoutImageShowViewBinding
import com.baidu.infinity.databinding.LayoutLayerItemBinding
import com.baidu.infinity.model.ImageModel
import com.baidu.infinity.model.LayerModel
import com.baidu.infinity.ui.fragment.home.shape.LayerManager
import com.baidu.infinity.ui.util.ImageModelManager
import com.baidu.infinity.ui.util.LayerModelManager
import com.baidu.infinity.ui.util.state.ImageState
import com.baidu.infinity.ui.util.state.LayerState
import com.bumptech.glide.Glide
import com.drake.brv.BindingAdapter
import com.drake.brv.listener.DefaultItemTouchCallback
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup

class BackgroundPopUpWindow(context: Context) {
    var mBinding: LayoutImageShowViewBinding ?= null
    val layoutInflater = LayoutInflater.from(context)
    var addBackgroundChangeListener: (resId: Int)->Unit = {}
    val popUpWindow: PopupWindow by lazy {
        mBinding = LayoutImageShowViewBinding.inflate(layoutInflater)
        PopupWindow(context).apply {
            contentView = mBinding!!.root
            mBinding!!.recyclerView.linear().setup {
                addType<ImageModel>(R.layout.layout_layer_item)
                onBind {
                    val binding = getBinding<LayoutLayerItemBinding>()
                    val data = getModel<ImageModel>()
                    Glide.with(binding.root)
                        .load(data.resIdRes)
                        .into(binding.imageView)
                    if(data.state == ImageState.NORMAL){
                        binding.selectView.visibility = View.INVISIBLE
                    }else{
                        binding.selectView.visibility = View.VISIBLE
                    }
                    binding.root.setOnClickListener{
                        ImageModelManager.instance.select(data)
                        addBackgroundChangeListener(data.resIdRes)
                        mBinding!!.recyclerView.models = ImageModelManager.instance.getImageList()
                    }

                }
            }.models = ImageModelManager.instance.getImageList()
            width = LayoutParams.WRAP_CONTENT
            height = LayoutParams.WRAP_CONTENT

        }
    }
    fun showAtLocation(parent: View,gravity: Int, x: Int, y: Int){
        popUpWindow.showAtLocation(parent,gravity,x, y)
    }
    fun showAsDropDown(anchor: View, xoff: Int, yoff: Int, gravity: Int){
        popUpWindow.showAsDropDown(anchor,xoff, yoff, gravity)
    }
    fun hide(){
        popUpWindow.dismiss()
    }
}